package com.ycMc.controller;

import com.ycMc.bean.request.InStockRequest;
import com.ycMc.bean.request.OrderRequest;
import com.ycMc.bean.response.ObjectResponse;
import com.ycMc.bean.response.PageBeanResponse;
import com.ycMc.common.bean.PageBean;
import com.ycMc.common.bean.Response;
import com.ycMc.constant.ExcelPoiUtil;
import com.ycMc.domain.InStock;
import com.ycMc.domain.Order;
import com.ycMc.service.InStockService;
import com.ycMc.service.OrderService;
import com.ycMc.utils.DateUtil;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileOutputStream;
import java.util.List;

/**
 * Created by libo2 on 2017/12/12.
 */
@RequestMapping("api/instock")
@Controller
public class InStockController {

    @Autowired
    private InStockService inStockService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("insert")
    @ResponseBody
    public Response insert(InStockRequest request){
        Response response = new Response();
        inStockService.insert(request);
        response.setStatusCode(Response.SUCCESS);
        return  response;
    }


    @RequestMapping("update")
    @ResponseBody
    public Response update(InStockRequest request){
        Response response = new Response();
        inStockService.update(request);
        response.setStatusCode(Response.SUCCESS);
        return  response;
    }


    @RequestMapping("delete")
    @ResponseBody
    public Response delete(InStockRequest request){
        Response response = new Response();
        inStockService.delete(request.getId());
        response.setStatusCode(Response.SUCCESS);
        return  response;
    }

    @RequestMapping("select")
    @ResponseBody
    public PageBeanResponse select(){
        return  null;
    }


    @RequestMapping("query")
    @ResponseBody
    public PageBeanResponse query(InStockRequest request){
        PageBeanResponse response = new PageBeanResponse();
        PageBean pageBean = inStockService.query(request);
        response.setStatusCode(Response.SUCCESS);
        response.setMsg("suucess");
        response.setPageBean(pageBean);
        return  response;
    }


    @RequestMapping("selectOne")
    @ResponseBody
    public ObjectResponse selectOne(InStockRequest request){
        ObjectResponse response = new ObjectResponse();

        InStock inStock = inStockService.selectOne(request.getId());
        response.setObject(inStock);
        response.setStatusCode(Response.SUCCESS);
        response.setMsg("success");

        return  response;
    }


    @RequestMapping("downloadInStockByQueryCondition")
    @ResponseBody
    public Response downloadInStockByQueryCondition(InStockRequest request){
        Response  response = new Response();

        PageBean pageBean = inStockService.query(request);
        // 第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("学生表一");
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = row.createCell(0);

        List<Order> orders =  orderService.selectAll();

        cell = row.createCell(0);
        cell.setCellValue("序号");
        cell.setCellStyle(style);
        cell = row.createCell(1);
        cell.setCellValue("产品名称");
        cell.setCellStyle(style);
        cell = row.createCell(2);
        cell.setCellValue("产品品牌");
        cell.setCellStyle(style);
        cell = row.createCell(3);
        cell.setCellValue("产品规格");
        cell.setCellStyle(style);
        cell = row.createCell(4);
        cell.setCellValue("创建时间");
        cell.setCellStyle(style);
        cell = row.createCell(5);
        cell.setCellValue("台数");
        cell.setCellStyle(style);
        cell = row.createCell(6);
        cell.setCellValue("入库价格");
        cell.setCellStyle(style);
        cell = row.createCell(7);
        cell.setCellValue("所属供应商");
        cell.setCellStyle(style);

        List<InStock> inStocks=pageBean.getList();
        int i =0;
        for(InStock instock:inStocks){
            row = sheet.createRow((int) i + 1);
            row.createCell(0).setCellValue(i);
            row.createCell(1).setCellValue(instock.getProductName());
            row.createCell(2).setCellValue(instock.getProductVersion());
            row.createCell(3).setCellValue(instock.getProductBrand());
            row.createCell(4).setCellValue(DateUtil.dateFormat3(instock.getCreateTime()));
            row.createCell(5).setCellValue(instock.getAddNumber());
            row.createCell(6).setCellValue(instock.getInStockPrice());
            row.createCell(7).setCellValue(instock.getSupplier());

            i++;
        }
        try
        {
            String fileName = "instockExcel"+System.currentTimeMillis()+".xls";
            String fileNameUrl = ExcelPoiUtil.Linux_url+fileName;
            FileOutputStream fout = new FileOutputStream(fileNameUrl);
            wb.write(fout);
            fout.close();
            response.setMsg("excelData/"+fileName);
            response.setStatusCode(Response.SUCCESS);

        }
        catch (Exception e)
        {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setStatusCode(Response.SUCCESS);
        }
        return response;
    }




}
